package com.example.demo.utils;

import com.example.demo.entity.File;
import com.example.demo.entity.Folder;
import com.example.demo.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class UserNameUtils {
    Url2Base64 url2Base64=new Url2Base64();
    public static String getUsername(){
        //获取用户名
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        return user.getUsername();
    }
    /**
     * 从1开始,因为不把用户名显示给前端/用户
     * @param fileList 将fileList中path的用户名去掉
     * @return
     */
    public List<File> removeUsername(List<File> fileList,String username) throws IOException {
        for (int j=0;j<fileList.size();j++){
            StringBuilder newpath = new StringBuilder();
            File file=fileList.get(j);
            String P1=file.getPicturePath1();
            String url;
            if(!(P1.equals(""))) {
                url="http://47.98.175.236:8088/"+P1;
                file.setPicturePath1(url2Base64.urlToBase64(url,username));
            }
            String P2=file.getPicturePath2();
            if(!(P2.equals(""))) {
                url="http://47.98.175.236:8088/"+P2;
                file.setPicturePath2(url2Base64.urlToBase64(url,username));
            }
            String P3=file.getPicturePath3();
            if(!(P3.equals(""))) {
                url="http://47.98.175.236:8088/"+P3;
                file.setPicturePath3(url2Base64.urlToBase64(url,username));
            }
            String filepath=file.getPath();
            String[] path=filepath.split("/");
            //从1开始,因为不把用户名显示给前端/用户
            for (int i = 1; i <= path.length - 1; i++) {
                if (i == 1) {
                    newpath.append(path[i]);
                } else {
                    newpath.append("/").append(path[i]);
                }
            }
            file.setPath(newpath.toString());
        }
        return fileList;
    }
//将folderList中path的用户名去掉
    public List<Folder> removeUsername2(List<Folder> folderList){
        for (int j=0;j<folderList.size();j++){
            StringBuilder newpath = new StringBuilder();
            Folder folder=folderList.get(j);
            String filepath=folder.getPath();
            String[] path=filepath.split("/");
            //从1开始,因为不把用户名显示给前端/用户
            for (int i = 1; i <= path.length - 1; i++) {
                if (i == 1) {
                    newpath.append(path[i]);
                } else {
                    newpath.append("/").append(path[i]);
                }
            }
            folder.setPath(newpath.toString());
        }
        return folderList;
    }

    /**
     * 搜素文件路径的下一级文件
     * @param fileList 文件的集合
     * @param a 路径由“/”分开后的长度+1；及下一级路径的长度
     */
    public List<File> nextLevel(List<File> fileList,int a){
        for (int j=0;j<fileList.size();j++){
            File file=fileList.get(j);
            String filepath=file.getPath();
            String[] path=filepath.split("/");
            if(path.length!=a) {
                fileList.remove(j);
                j--;
            }
        }
        return fileList;
    }
//搜素文件夹路径的下一级文件， folder里查询，返回下一级文件/文件夹
    public List<Folder> nextLevel2(List<Folder> folderList,int a){
        for (int j=0;j<folderList.size();j++){
            Folder folder =folderList.get(j);
            String filepath=folder.getPath();
            String[] path=filepath.split("/");
            if(path.length!=a) {
                folderList.remove(j);
                j--;
            }
        }
        return folderList;
    }
//删除folder与file里面相同的文件。返回剩余的下一级文件夹
    public List<Folder> removeFile(List<Folder> folderList,List<File> fileList){
        int b=0;
        List<Object> list=new ArrayList<>();
        for (int j=0;j<folderList.size();j++) {
            Folder folder = folderList.get(j);
            String filepath = folder.getPath();
            for (int i = 0; i < fileList.size(); i++) {
                File file=fileList.get(i);
                String filepath2=file.getPath();
                if(filepath.equals(filepath2)){
                    list.add(j);
                    //folderList.remove(j);
                }
            }
        }
        for (int i = 0; i < list.size(); i++) {
            int a=(int)list.get(i);
            if(b!=0){
                a=a-b;
            }
            b++;
            folderList.remove(a);
        }//控制a，b删除folderList中相同路径的文件
        return folderList;
    }
}
